// //index.js
const app = getApp()

var Util = require("../../utils/utils")

Page({
  data: {
    baseURL: app.globalData.baseURL,

    // 宫格
    gridBorder: true,
    typeList: [{
      icon: 'cardboardfill',
      color: 'red',
      badge: 120,
      name: 'VR'
    }, {
      icon: 'recordfill',
      color: 'orange',
      badge: 1,
      name: '录像'
    }, {
      icon: 'picfill',
      color: 'yellow',
      badge: 0,
      name: '图像'
    }, {
      icon: 'noticefill',
      color: 'olive',
      badge: 22,
      name: '通知'
    }, {
      icon: 'upstagefill',
      color: 'cyan',
      badge: 0,
      name: '排行榜'
    }, {
      icon: 'clothesfill',
      color: 'blue',
      badge: 0,
      name: '皮肤'
    }, {
      icon: 'discoverfill',
      color: 'purple',
      badge: 0,
      name: '发现'
    }, {
      icon: 'questionfill',
      color: 'mauve',
      badge: 0,
      name: '帮助'
    }, {
      icon: 'commandfill',
      color: 'purple',
      badge: 0,
      name: '问答'
    }, {
      icon: 'brandfill',
      color: 'mauve',
      badge: 0,
      name: '版权'
    }],
    gridCol: 3,
    skin: false,
    // 卡片
    cardCur: 0,
    swiperList: [{
      id: 0,
      type: 'image',
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big84000.jpg'
    }, {
      id: 1,
      type: 'image',
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big84001.jpg',
    }, {
      id: 2,
      type: 'image',
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39000.jpg'
    }, {
      id: 3,
      type: 'image',
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg'
    }, {
      id: 4,
      type: 'image',
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big25011.jpg'
    }, {
      id: 5,
      type: 'image',
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big21016.jpg'
    }, {
      id: 6,
      type: 'image',
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big99008.jpg'
    }],
    // 最新的时间定义
    dayNew: 3,
    // 全部文章
    allList: [],
    // 最新文章
    // 热点文章
    // 高难文章
    newList: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    swiperImages: [],
    // 底部tabbar标识
    active: 0,
    // 展示弹出框
    show: false,
    // 展示内容
    content: '',
    // 题目类型
    questionType: [],
    imageURL: "../../../../images/a.jfif"
  },

  /** ColorUI 功能 */
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },

  // 文章卡片
  isCard(e) {
    this.setData({
      isCard: e.detail.value
    })
  },



  /**上面为 ColorUI 功能 */
  /**以下自定义功能 */
  tabChange(data) {
    // console.log(data.detail);
    let {
      title
    } = data.detail;
    // 拉取全部文章并刷新当前tab的文章
    this.getAllLit(title);
  },

  // 获取全部文章
  getAllLit(title) {
    // 获取全部文章
    var that = this;
    var url = `${app.globalData.baseURL}/api/question/question/list?page=1&limit=1000`;
    wx.request({
      url,
      success(res) {
        // console.log(res.data.page.list);
        var allList = res.data.page.list;
        allList = allList.map(e => {
          e.createTime = Util.formatTime(new Date(e.createTime));
          e.updateTime = Util.formatTime(new Date(e.updateTime));
          return e;
        })
        var newList = [];
        // 获取最新文章
        if (title == "最新文章") {
          allList.map(e => {
            if (Util.dayDiff(e.createTime) <= that.data.dayNew) {
              newList.push(e);
            }
          });
        } else if (title == "热点文章") {

        } else if (title == "高难文章") {

        } else if (title == "必读文章") {

        }
        that.setData({
          allList,
          newList
        });
      }
    });
  },

  // 前往详情页
  goQuestionShow(data) {
    // console.log(data.currentTarget.dataset);
    // 跳转详情页
    var url = `../questionShow/index?id=${data.currentTarget.dataset.id}`;
    wx.navigateTo({
      url,
      success: function (res) {
        // console.log(res);
        // console.log("跳转成功");
      },
      fail(err) {
        // console.log(err)
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // console.log("share")
  },

  // 页面加载时触发
  onLoad() {
    // console.log(app.globalData.baseURL);
    // 初始化 ColorUI 轮播数组
    this.towerSwiper('swiperList');

    // 获取题目类型
    var that = this;
    let url = `${app.globalData.baseURL}/api/question/type/list?page=1&limit=10`;
    wx.request({
      url,
      success(res) {
        let {
          list
        } = res.data.page;
        // console.log(list);
        let {
          typeList
        } = that.data;
        // console.log(typeList);

        // 将 list 的中元素 e 的 id 和 name 覆盖 typeList 的值
        list.forEach((e, i) => {
          typeList[i]["id"] = e.id;
          typeList[i]["name"] = e.type;
        });
        
        // console.log(typeList);

        that.setData({
          typeList
        })
      }
    });

  },

  // 设置自定义tabbar，active显示属性
  onShow() {
    // console.log("主页tab");
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        active: 0
      })
    }
  },

  onClose() {
    this.setData({
      show: false
    });
  },

  toQuestionList(data) {
    // console.log(data.currentTarget.dataset.id);
    var url = `../question/index?id=${data.currentTarget.dataset.id}`;
    wx.navigateTo({
      url,
      success: function (res) {
        // console.log(res);
        // console.log("跳转成功");
      },
      fail(err) {
        // console.log(err)
      }
    })

  }
})